<?php
class ModelAccountMapping extends Model {
	public function addIdentity($data) {
		$sql = "INSERT INTO " . DB_PREFIX . "customer_mapping SET 
		customer_id = '" . $this->db->escape($data['customer_id']) . "',
		identity = '" . $this->db->escape($data['identity']) . "', 
		provider = '" . $this->db->escape($data['provider']) . "'";
		
		$this->db->query($sql);
	}
	
	public function add_customer($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "customer (firstname, lastname, email, telephone, fax, newsletter, customer_group_id, password, status, date_added) VALUES ('" . $this->db->escape($data['firstname']) . "', '" . $this->db->escape($data['lastname']) . "', '" . $this->db->escape($data['email']) . "', '', '', '0', '" . (int)$data['customer_group_id'] . "', '" . $this->db->escape(md5($data['password'])) . "', '1', NOW())");

		return $this->db->getLastId(); // customer_id
	}
	
	public function deleteIdentity($identity) {
		$sql = "DELETE FROM " . DB_PREFIX . "customer_mapping WHERE identity = '" . $this->db->escape($identity) . "'";
		
		$this->db->query($sql);
	}
	
	public function validIdentity($identity) {
		$sql = "SELECT * FROM " . DB_PREFIX . "customer_mapping cm LEFT JOIN " . DB_PREFIX . "customer c ON cm.customer_id = c.customer_id WHERE cm.identity = '" . $this->db->escape($identity) . "'";
		//echo $sql;
		$query = $this->db->query($sql);

		if ($query->num_rows) {
			return $query->row;
    	} else {
      		return FALSE;
    	}
	}
	
	public function getPolicyUrl() {
		if ($this->config->get('config_account_id')) {
			$url = HTTP_SERVER . 'index.php?route=information/information&information_id=' . $this->config->get('config_account_id');
		} else {
			$url = HTTP_SERVER;
		}
		return $url;
	}
	
	public function formUrl($url) {
		$urls = parse_url($url);
		$scheme = 'http';
		if (empty($urls['scheme'])) {
			$urls = parse_url($scheme.'://'.$url);
		} else {
			$scheme = $urls['scheme'];
		}
		
		if (empty($urls['host'])) {
			$host = '';
		} else {
			$host = $urls['host'];
		}
		
		if (empty($urls['path'])) {
			$path = '/';
		} else {
			$path = $urls['path'];
		}
		
		if ('/' != $path) {
			$path = ereg_replace('/$', '', $path);
		}
		
		if (empty($urls['query'])) {
			$query = '';
		} else {
			$query = '?'.$urls['query'];
		}
		
		return strtolower($scheme.'://'.$host).$path.$query;
	}
	
	public function getAssociationHandle($url) {
		$request = array();
		$request['openid.ns'] = 'http://specs.openid.net/auth/2.0';
		$request['openid.mode'] = 'associate';
		$request['openid.assoc_type'] = 'HMAC-SHA1';
		$request['openid.session_type'] = 'no-encryption';
		$url = $url.'?'.http_build_query($request);
		
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		$contents = curl_exec($curl);
		curl_close($curl);
		
		$assoc_handle = null;
		$lines = explode("\n", $contents);

		foreach($lines as $line){
			if(substr($line, 0, 13) == "assoc_handle:"){
				$assoc_handle = substr($line, 13);
				break;
			}
		}
		return $assoc_handle;
	}
	
	//fetches an endpoint from Google
	public function getEndPoint($url) {
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		$contents = curl_exec($curl);
		curl_close($curl);
		
		$dom = new DOMDocument();
		$dom->loadXML($contents);
		$uri = $dom->getElementsByTagName('URI');
		$uri = $uri->item(0)->nodeValue;
		return $uri;
	}
	
	public function getLiveID($url, $header) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		curl_setopt($curl, CURLOPT_TIMEOUT, 60);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$contents = curl_exec($curl);
		curl_close($curl);
		
		try {
			$xml = new SimpleXMLElement($contents);
		} catch(Exception $e) {
			return FALSE;
		}
		return $xml;
	}


	public function getOpenIDServer($url) {
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
		curl_setopt($curl, CURLOPT_HEADER, TRUE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_HTTPGET, FALSE);
		curl_setopt($curl, CURLOPT_POST, TRUE);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		$contents = curl_exec($curl);
		curl_close($curl);

		if (!empty($contents)) {
			$s = array();
			
			preg_match("/\<link rel=\"openid\.server\" href=\"([^\"]+)\"( )?(\/)?\>/i", $contents, $matches);
			if(!empty($matches)) $s[1] = $matches[1];
			
			preg_match("/\<link rel=\"openid2\.provider\" href=\"([^\"]+)\"( )?(\/)?\>/i", $contents, $matches);
			if(!empty($matches)) $s[2] = $matches[1];
			
			return $s;
		} else {
			return FALSE;
		}
	}
}
?>